<?php
require("menuBarHead.php");
require("db_manage/dbFunctions.php");

$resource_name = $_POST['select'];

	$dbh = connectDB();

	if( strcmp($resource_name,"All") == 0 )
	{
		require("getAllResourcesAvailable.php");
		print( getAllResourcesAvailable() );
	}
else
{
	if(strcmp($resource_name,"Rooms") == 0)
	{
		$query = "SELECT qr_code,building,floor,room_number,room_type ".
			 "FROM Room ".
			 "WHERE qr_code NOT IN (SELECT DISTINCT qr_code FROM Resource_Designation WHERE has_been_checked_in = 0);";
	
		$stmt = $dbh->prepare($query);
		$stmt->execute();
	}
	else
	{//specified resoruce is an equipment
		$query = "SELECT qr_code,equipment_name,serial_number,current_condition ".
		 	 "FROM Equipment ".
		 	 "WHERE equipment_name=:equip AND qr_code NOT IN (SELECT DISTINCT qr_code FROM Resource_Designation WHERE has_been_checked_in = 0)";
	

		$stmt = $dbh->prepare($query);
		$stmt->bindParam(':equip',$resource_name);
		$stmt->execute();
	}

	$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

	if($stmt->rowCount() == 0)
		print ("<h2>There are no $resource_name(s) available. All $resource_name(s) are currently checked out.</h3>");
	else if(strcmp($resource_name,"Rooms") == 0)
	{
		print ("<h2>Available $resource_name(s):</h1><br>");

		print ("<table border=1>");
		print ("<tr><td><strong>QR Code</strong></td>
		    <td><strong>Building Name</strong></td>
		    <td><strong>Floor Number</strong></td>
		    <td><strong>Room Number</strong></td>
		    <td><strong>Room Type</strong></td></tr>");
		foreach($result as $room )
		{
			print ("<tr>
				<td>".$room['qr_code']."</td>
			    	<td>".$room['building']."</td>
				<td>".$room['floor']."</td>
				<td>".$room['room_number']."</td>
			    	<td>".$room['room_type']."</td>
			     </tr>");
		}
		print ("</table>");
	}
	else
	{
		print ("<h2>Available $resource_name(s):</h1><br>");

		print ("<table border=1>");
		print ("<tr><td><strong>QR Code</strong></td>
		    <td><strong>Equipment Name</strong></td>
		    <td><strong>Serial Number</strong></td>
		    <td><strong>Current Condition</strong></td></tr>");
		foreach($result as $equipment )
		{
			print ("<tr>
				<td>".$equipment['qr_code']."</td>
				<td>".$equipment['equipment_name']."</td>
				<td>".$equipment['serial_number']."</td>
				<td>".$equipment['current_condition']."</td>
			      </tr>");

		}
		print ("</table>");
	}

}

require("menuBarClose.php");
?>
